﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class ChiTietLapTopDAO
    {
        #region 1   -   Queries

        public static ChiTietLapTopDTO LayChiTietLapTopTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_LAP_TOP chiTietLapTop = myDB.CHI_TIET_LAP_TOPs.Single(c => c.ID == id);
            if (chiTietLapTop != null)
            {
                return ChuyenTuChiTietLapTopSangChiTiepLapTopDTO(chiTietLapTop);
            }
            return null;
        }

        public static ChiTietLapTopDTO LayChiTietLapTopTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_LAP_TOP chiTietLapTop = myDB.CHI_TIET_LAP_TOPs.Single(c => c.ID == masanpham);
            if (chiTietLapTop != null)
            {
                return ChuyenTuChiTietLapTopSangChiTiepLapTopDTO(chiTietLapTop);
            }
            return null;
        }
        public static List<ChiTietLapTopDTO> TimKiemLapTopNangCao(string tenhangsanxuat, string thongtincpu,
           string tocdocputu, string tocdocpuden, string kichthuocmanhinh, string dungluongvgatu,
           string dungluongvgaden, string dungluongramtu, string dungluongramden, string dungluonghddtu,
           string dungluonghddden, string dungluongpin, string thongtinhedieuhanh, string trongluongmaytu,
           string trongluongmayden)
        {
            float ftocdocputu = ChuyenTocDoCPU(tocdocputu);
            float ftocdocpuden = ChuyenTocDoCPU(tocdocpuden);
            float fdungluongvgatu = ChuyenDungLuongVGA(dungluongvgatu);
            float fdungluongvgaden = ChuyenDungLuongVGA(dungluongvgaden);
            float framtu = ChuyenDungLuongRAM(dungluongramtu);
            float framden = ChuyenDungLuongRAM(dungluongramden);
            float fhddtu = ChuyenDungLuongHDD(dungluonghddtu);
            float fhddden = ChuyenDungLuongHDD(dungluonghddden);
            float ftrongluongmaytu = ChuyenTrongLuongMay(trongluongmaytu);
            float ftrongluongmayden = ChuyenTrongLuongMay(trongluongmayden);
            //dungluongpin = ChuyenPin(dungluongpin);
            //kichthuocmanhinh = ChuyenKichThuocManHinh(kichthuocmanhinh);
            if (thongtinhedieuhanh.Contains("Dos"))
                thongtinhedieuhanh = "Dos";

            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            var listctlt = from ctlt in myDB.CHI_TIET_LAP_TOPs
                           select ctlt;
            result = ChuyenTuIQueryableSangList(listctlt);
            //IQueryable<CHI_TIET_LAP_TOP> listctlt = from ctlt in myDB.CHI_TIET_LAP_TOPs
            //                                           select ctlt;
            if (tenhangsanxuat != "Tùy ý")
            {
                HangSanXuatDTO hangsanxuat = HangSanXuatDAO.LayHangSanXuatTheoTenHangSanXuatVaNhomSanPham(tenhangsanxuat,1);
                int mahangsanxuat = hangsanxuat.ID;
                listctlt = from ctlt in myDB.CHI_TIET_LAP_TOPs
                           where ctlt.SAN_PHAM.MaHangSanXuat == mahangsanxuat
                           select ctlt;
                result = ChuyenTuIQueryableSangList(listctlt);
            }

            if (thongtincpu != "Tùy ý")
            {
                listctlt = from ctlt in listctlt
                           where ctlt.ThongTinCPU.Contains(thongtincpu)
                           select ctlt;
                result = ChuyenTuIQueryableSangList(listctlt);
            }

            if (kichthuocmanhinh != "Tùy ý")
            {
                listctlt = from ctlt in listctlt
                           where ctlt.KichThuocManHinh.Contains(kichthuocmanhinh)
                           select ctlt;
                result = ChuyenTuIQueryableSangList(listctlt);
            }

            if (dungluongpin != "Tùy ý")
            {
                listctlt = from ctlt in listctlt
                           where ctlt.Pin.Contains(dungluongpin)
                           select ctlt;
                result = ChuyenTuIQueryableSangList(listctlt);
            }

            if (thongtinhedieuhanh != "Tùy ý")
            {
                listctlt = from ctlt in listctlt
                           where ctlt.ThongTinHeDieuHanh.Contains(thongtinhedieuhanh)
                           select ctlt;
                result = ChuyenTuIQueryableSangList(listctlt);
            }

            result = SoSanhTocDoCPU(result, ftocdocputu, ftocdocpuden);
            result = SoSanhDungLuongVGA(result, fdungluongvgatu, fdungluongvgaden);
            //result = SoSanhDungLuongRAM(result, framtu, framden);
            result = SoSanhDungLuongHDD(result, fhddtu, fhddden);
            result = SoSanhTrongLuongMay(result, ftrongluongmaytu, ftrongluongmayden);
          

            return result;
        }

        public static List<ChiTietLapTopDTO> ChuyenTuIQueryableSangList(IQueryable<CHI_TIET_LAP_TOP> listctlt)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            foreach (CHI_TIET_LAP_TOP item in listctlt)
            {
                result.Add(ChuyenTuChiTietLapTopSangChiTiepLapTopDTO(item));
            }
          
            return result;
        }

       
        public static List<ChiTietLapTopDTO> SoSanhTocDoCPU(List<ChiTietLapTopDTO> list, float f1, float f2)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                if (ChuyenTocDoCPU(list[i].TocDoCPU) >= f1 &&
                    ChuyenTocDoCPU(list[i].TocDoCPU) <= f2)
                    result.Add(list[i]);
            }
            if (result.Count == 0)
                return list;
            return result;
        }

        public static List<ChiTietLapTopDTO> SoSanhDungLuongVGA(List<ChiTietLapTopDTO> list, float f1, float f2)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                if (ChuyenDungLuongVGA(list[i].DungLuongVGA) >= f1 &&
                    ChuyenDungLuongVGA(list[i].DungLuongVGA) <= f2)
                    result.Add(list[i]);

            }
            if (result.Count == 0)
                return list;
            return result;
        }

        public static List<ChiTietLapTopDTO> SoSanhDungLuongRAM(List<ChiTietLapTopDTO> list, float f1, float f2)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                if (ChuyenDungLuongRAM(list[i].DungLuongRAM) >= f1 &&
                    ChuyenDungLuongRAM(list[i].DungLuongRAM) <= f2)
                    result.Add(list[i]);

            }
            if (result.Count == 0)
                return list;
            return result;
        }

        public static List<ChiTietLapTopDTO> SoSanhDungLuongHDD(List<ChiTietLapTopDTO> list, float f1, float f2)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                if (ChuyenDungLuongHDD(list[i].DungLuongHDD) >= f1 &&
                    ChuyenDungLuongHDD(list[i].DungLuongHDD) <= f2)
                    result.Add(list[i]);

            }
            if (result.Count == 0)
                return list;
            return result;
        }

        public static List<ChiTietLapTopDTO> SoSanhTrongLuongMay(List<ChiTietLapTopDTO> list, float f1, float f2)
        {
            List<ChiTietLapTopDTO> result = new List<ChiTietLapTopDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                if (ChuyenTrongLuongMay(list[i].TrongLuongMay) >= f1 &&
                    ChuyenTrongLuongMay(list[i].TrongLuongMay) <= f2)
                    result.Add(list[i]);

            }
            if (result.Count == 0)
                return list;
            return result;
        }

        public static string ChuyenPin(string s)
        {
            if (s.Contains("3"))
                return "3";
            if (s.Contains("6"))
                return "6";
            if (s.Contains("9"))
                return "9";
            return s;
        }

        public static string ChuyenKichThuocManHinh(string s)
        {
            if (s.Contains("13"))
                return "13";
            if (s.Contains("14"))
                return "14";
            if (s.Contains("15"))
                return "15";
            return s;
        }
        public static float ChuyenTocDoCPU(string s)
        {
            if (s == null)
                return 0;
            s = s.Replace("GHz", "");
            s = s.Replace("Ghz", "");
            s = s.Replace("GH", "");
            s = s.Replace("Gh", "");
            s = s.Replace("MHz", "");
            s = s.Replace("Mhz", "");
            s = s.Replace("*", "");
            s = s.Replace("GB", "");
            s = s.Replace("MB", "");
           
            s = Bo2DauCham(s);
            s = s.Replace(" ", "");

            return float.Parse(s);
        }

        public static string Bo2DauCham(string s)
        {
            string result = s;
            int Count = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '.')
                    Count++;
            }
            for (int i = 1; i < Count; i++)
            {
                result = result.Remove(result.IndexOf("."));
            }
            return result;
        }
        public static float ChuyenDungLuongVGA(string s)
        {
            if (s == null)
                return 0;
            if (s.Contains("MB"))
                s = s.Replace("MB", "");
            if (s.Contains("GB"))
                s = s.Replace("GB", "000");
            s = s.Replace("(", "");
            s = s.Replace(")", "");
            s = s.Replace("+", "");
            s = s.Replace("*", "");
            s = s.Replace(" ", "");
            s = Bo2DauCham(s);
            return float.Parse(s);
        }

        public static float ChuyenDungLuongRAM(string s)
        {
            return ChuyenDungLuongVGA(s);
        }

        public static float ChuyenDungLuongHDD(string s)
        {
            if (s == null)
                return 0;
            s = s.Replace("GB", "");
            s = s.Replace(" ", "");
            return float.Parse(s);
        }

        public static float ChuyenTrongLuongMay(string s)
        {
            if (s == null)
                return 0;
            s = s.Replace("kg", "");
            s = s.Replace("Kg", "");
            s = s.Replace("KG", "");
            s = s.Replace(" ", "");
            return float.Parse(s);
        }
        public static bool ThemChiTietLapTop(ChiTietLapTopDTO chitietlaptopDTO)
        {
            CHI_TIET_LAP_TOP chiTiepLapTop = ChuyenTuChiTietLapTopDTOSangChiTietLapTop(chitietlaptopDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.CHI_TIET_LAP_TOPs.InsertOnSubmit(chiTiepLapTop);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaChiTietLapTopTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_LAP_TOP chiTietLapTop = myDB.CHI_TIET_LAP_TOPs.Single(c => c.ID == id);
            try
            {
                myDB.CHI_TIET_LAP_TOPs.DeleteOnSubmit(chiTietLapTop);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaChiTietLapTopTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_LAP_TOP chiTietLapTop = myDB.CHI_TIET_LAP_TOPs.Single(c => c.ID == masanpham);
            try
            {
                myDB.CHI_TIET_LAP_TOPs.DeleteOnSubmit(chiTietLapTop);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion

        #region 2   -   Function Helper

        public static ChiTietLapTopDTO ChuyenTuChiTietLapTopSangChiTiepLapTopDTO(CHI_TIET_LAP_TOP chitietlaptop)
        {
            return new ChiTietLapTopDTO(chitietlaptop.ID, chitietlaptop.MaSanPham.Value,
                chitietlaptop.ThongTinCPU, chitietlaptop.TocDoCPU, chitietlaptop.KichThuocManHinh,
                chitietlaptop.VGA, chitietlaptop.DungLuongVGA, chitietlaptop.RAM,
                chitietlaptop.DungLuongRAM, chitietlaptop.ThongTinHDD, chitietlaptop.DungLuongHDD,
                chitietlaptop.Pin, chitietlaptop.ThongTinMainBoard, chitietlaptop.ThongTinLAN,
                chitietlaptop.ThongTinWifi, chitietlaptop.ThongTinHeDieuHanh, chitietlaptop.ThongTinBaoHanh,
                chitietlaptop.TrongLuongMay, chitietlaptop.CacThongTinKhac);

        }

        public static CHI_TIET_LAP_TOP ChuyenTuChiTietLapTopDTOSangChiTietLapTop(ChiTietLapTopDTO chitietlaptopDTO)
        {
            CHI_TIET_LAP_TOP result = new CHI_TIET_LAP_TOP();
            result.CacThongTinKhac = " ";
            result.DungLuongHDD = chitietlaptopDTO.DungLuongHDD;
            result.DungLuongRAM = chitietlaptopDTO.DungLuongRAM;
            result.DungLuongVGA = chitietlaptopDTO.DungLuongVGA;
            result.ID = chitietlaptopDTO.ID;
            result.KichThuocManHinh = chitietlaptopDTO.KichThuocManHinh;
            result.MaSanPham = chitietlaptopDTO.MaSanPham;
            result.Pin = chitietlaptopDTO.Pin;
            result.RAM = chitietlaptopDTO.RAM;
            result.ThongTinBaoHanh = chitietlaptopDTO.ThongTinBaoHanh;
            result.ThongTinCPU = chitietlaptopDTO.ThongTinCPU;
            result.ThongTinHDD = chitietlaptopDTO.ThongTinHDD;
            result.ThongTinHeDieuHanh = chitietlaptopDTO.ThongTinHeDieuHanh;
            result.ThongTinLAN = chitietlaptopDTO.ThongTinLAN;
            result.ThongTinMainBoard = chitietlaptopDTO.ThongTinMainBoard;
            result.ThongTinWifi = chitietlaptopDTO.ThongTinWifi;
            result.TocDoCPU = chitietlaptopDTO.TocDoCPU;
            result.TrongLuongMay = chitietlaptopDTO.TrongLuongMay;
            result.VGA = chitietlaptopDTO.VGA;
            return result;
        }

        #endregion
    }
}
